package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zhangfujing
 * @date 2024/3/11 11:52
 */
@Slf4j
@Api(tags = "数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 根据id获取该id下所有的子节点
     * 这样不需要一次查出来全部的，前端实现点什么查什么
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> data= dictService.findChildData(id);
        return Result.ok(data);
    }

    /**
      导出数据字典接口**/
    @ApiOperation("导出")
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
       // return Result.ok();
    }

    /**
     导入数据字典接口**/
    @ApiOperation("导入")
    @PostMapping("/importData")
    public Result importDict(MultipartFile file){
        try {
            dictService.importDictData(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    /**
     * 省市区不需要上级编码，医院等级需要上级编码
     *
     * @param dictCode 上级编码
     * @param value          值
     * @return 数据字典名称
     */
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{dictCode}/{value}")
    public String getName(@ApiParam(name = "dictCode", value = "上级编码", required = true)
                          @PathVariable("dictCode") String dictCode,
                          @ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        return dictService.getDictName(dictCode, value);
    }

    @ApiOperation(value = "获取数据字典名称")
    @ApiImplicitParam(name = "value", value = "值", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/getName/{value}")
    public String getName(@ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        return dictService.getDictName("", value);
    }

    //根据DictCode获取下级节点
    @ApiOperation(value = "根据DictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> list=dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}
